package com.wjk.kylin.modules.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.modules.system.user.entity.UserPosition;
import com.wjk.kylin.modules.system.user.mapper.UserPositionMapper;
import com.wjk.kylin.modules.system.user.service.IUserPositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 用户与岗位关联表 服务实现类
 * </p>
 */
@Service
@Transactional(readOnly=true)
public class UserPositionServiceImpl extends ServiceImpl<UserPositionMapper, UserPosition> implements IUserPositionService {

    @Override
    @Transactional(readOnly=false)
    public void deleteByUserIds(List<String> userIds) {
        QueryWrapper<UserPosition> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userIds);
        super.remove(wrapper);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteByPositionIds(List<String> positionIds) {
        QueryWrapper<UserPosition> wrapper = new QueryWrapper<>();
        wrapper.in("position_id", positionIds);
        super.remove(wrapper);
    }

    @Override
    public List<String> findPositionListByUserId(String userId) {
        QueryWrapper<UserPosition> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userId);

        List<UserPosition> list = super.list(wrapper);

        return list.stream().map(UserPosition::getPositionId).distinct().collect(Collectors.toList());
    }

    @Override
    public void save(String userId, List<String> positionIds) {
        List<UserPosition> userPositionList = new ArrayList<>();
        positionIds.forEach(positionId->{
            UserPosition userPosition = new UserPosition();

            userPosition.setPositionId(positionId);
            userPosition.setUserId(userId);

            userPositionList.add(userPosition);
        });

        super.saveBatch(userPositionList);
    }
}
